home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / PXDTUT5.ZIP / PXDTUT5.PAS < prev    next >
Pascal/Delphi Source File  |  1997-08-14  |  13KB  |  547 lines

  1. Program VESA_UNIT;
  2.  
  3. uses
  4.  crt;
  5.  
  6. CONST
  7.  HEXTABLE : Array[0..15] of char
  8.  = ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
  9.  
  10. type
  11.  ListOfAvailModesT = Array[0..255] of word; {terminated by -1 ($FFFF) }
  12.  ListOfAvailModesP = ^ListOfAvailModesT;
  13.  
  14.  String2 = String[2];
  15.  String4 = String[4];
  16.  
  17.  
  18.  VESAInfoT = record
  19.               VESASignature : array[0..3] of byte;
  20.               VESAVersion   : word;
  21.               OEMStringPtr  : Pchar;
  22.               Capabilities  : array[0..3] of byte;
  23.               VideoModePtr  : ListOfAvailModesP;
  24.               TotalMemory   : word;
  25.               Reserved      : Array[0..235] of byte;
  26.              end;
  27.  
  28.  ModeAttributesT = (Available,          {mode avail on current hardware config..}
  29.                    Reserved,            {VESA 1.2 and above this is set to 1}
  30.                    BIOSFunctionsSupport,{Scroll, TTY output and pixel output}
  31.                    color,
  32.                    graphic,
  33.                    bit5,
  34.                    bit6,
  35.                    bit7,
  36.                    bit8);
  37.  
  38.  
  39.  WindowAttributesT  = (Supported,R,W);   {rest of the bits are unused...}
  40.  
  41.  
  42.  
  43.  
  44.  VESAModeInfoT = record
  45.                    ModeAttributes     : set of ModeAttributesT;
  46.                    WinAAttributes     : set of WindowAttributesT;
  47.                    WinBAttributes     : set of WindowAttributesT;
  48.                    WinGranularity     : word;
  49.                    WinSize            : word;
  50.                    WinASegment        : word;
  51.                    WinBSegment        : word;
  52.                    BankSwitch         : procedure;
  53.                    BytesPerScanLine   : word;
  54.  
  55.                    {Xtended Information}
  56.                    XResolution        : word;
  57.                    YResolution        : word;
  58.                    XCharSize          : byte;
  59.                    YCharSize          : byte;
  60.                    NumberOfPlanes     : byte;
  61.                    BitsPerPixel       : byte;
  62.                    NumberOfBanks      : byte;
  63.                    MemoryModel        : byte;
  64.                    BankSize           : byte;
  65.                    NumberOfImagePages : byte;
  66.  
  67.                    Reserved           : Array[0..225] of byte;
  68.                    {reserved is for something we won't think about now...
  69.                     true color and stuff...                               }
  70.                  end;
  71.  
  72.  
  73. var
  74.  VESAInfo          : VESAInfoT;
  75.  VESAModeInfo      : VESAModeInfoT;
  76.  
  77.  Cur_page          : word;
  78.  Bankmult          : word;
  79.  BankShiftModifier : word;
  80.  
  81.  VESAModeList : Array[0..40] of word;
  82.  
  83.  counter : word;
  84.  
  85.  
  86.  
  87. Procedure Error(msg : string);
  88. begin
  89.  asm
  90.   mov ax,03h
  91.   int 10h
  92.  end;
  93.  
  94.  Writeln(msg);
  95.  HALT;
  96. end;
  97.  
  98.  
  99. function Byte2Hex(b : byte) : String2;
  100. var
  101.  temp : string2;
  102. begin
  103.  temp[1] := HEXTABLE[b shr 4];
  104.  temp[2] := HEXTABLE[b mod 16];
  105.  Byte2Hex := temp;
  106. end;
  107.  
  108. function Word2Hex(w : word) : String4;
  109. begin
  110.   Word2Hex:=Byte2Hex(hi(w))+Byte2Hex(lo(w));
  111. end;
  112.  
  113.  
  114.  
  115. FUNCTION GetVESAInfo : boolean;
  116. Assembler;
  117. asm
  118.  mov ah,4Fh
  119.  mov al,00h
  120.  lea di,VESAInfo
  121.  int 10h
  122.  cmp ah,0
  123.  jne @fail
  124.  mov ax,1
  125.  jmp @out
  126. @fail:
  127.  mov ax,0
  128. @out:
  129. end;
  130.  
  131. FUNCTION GetVESAModeInfo(mode : word) : boolean;
  132. Assembler;
  133. asm
  134.  mov ah,4Fh
  135.  mov al,01h
  136.  xor cx,cx
  137.  mov cx,mode
  138.  lea di,VESAModeInfo
  139.  int 10h
  140.  cmp ah,0
  141.  jne @fail
  142.  mov ax,1
  143.  jmp @out
  144. @fail:
  145.  mov ax,0
  146. @out:
  147. end;
  148.  
  149.  
  150. Procedure SetBank(nr : word);
  151. Assembler;
  152. asm
  153.  xor bl,bl                       {Window 0 selected}
  154.  mov dx,nr
  155.  call [VESAModeInfo.BankSwitch]
  156.  mov dx,nr                       {dx is destroyed by far procedure call}
  157.  mov cur_page,dx                 {update global page variable}
  158. end;
  159.  
  160. Procedure SetLogicalScanline(width : word);
  161. Assembler;
  162. asm
  163.  mov ah,4Fh
  164.  mov al,06h
  165.  mov bl,0
  166.  mov cx,width
  167.  int 10h
  168. end;
  169.  
  170.  
  171. Procedure SetScreenPosition(x,y : word);
  172. Assembler;
  173. asm
  174.  mov ah,4Fh
  175.  mov al,07h
  176.  xor bx,bx
  177.  mov cx,[x]
  178.  mov dx,[y]
  179.  int 10h
  180. end;
  181.  
  182.  
  183. FUNCTION SetVESAMode(mode : word) : boolean;
  184. Assembler;
  185. asm
  186.  mov ax,03h
  187.  int 10h              {start from textmode}
  188.  
  189.  mov ah,4Fh
  190.  mov al,02h
  191.  mov bx,mode
  192.  int 10h
  193.  cmp ah,0
  194.  jne @fail            {set the VESA mode through VESA function 02h}
  195.  
  196.  mov ah,4Fh
  197.  mov al,05h
  198.  xor bx,bx
  199.  mov dx,0
  200.  int 10h
  201.  mov cur_page,0       {initialize global page variable}
  202.  cmp ah,0
  203.  jne @fail            {Set bank 0}
  204.  
  205.  mov ax,1             {Set call to succesful}
  206.  jmp @out
  207. @fail:
  208.  mov ax,0
  209. @out:
  210. end;
  211.  
  212.  
  213. PROCEDURE   VESAputpixel(x, y : WORD; c : BYTE);
  214. VAR
  215.   bank   : WORD;
  216.   offs   : longint;
  217. BEGIN
  218.  
  219.   offs := LONGINT(y) * VESAModeInfo.Xresolution + x;
  220.   bank := offs SHR (16-BankShiftModifier);
  221.   offs := offs - (bank SHL (16-BankShiftModifier));
  222.  
  223.   IF bank <> Cur_page THEN {page = global var - active page}
  224.   BEGIN
  225.     cur_page := bank;
  226.     ASM
  227.       Xor bl,bl
  228.       mov dx,bank
  229.       call [VESAModeInfo.BankSwitch]
  230.     END;
  231.    END;
  232.  
  233.   ASM
  234.     MOV AX, $A000
  235.     MOV ES, ax
  236.     MOV DI, WORD(offs)
  237.     MOV AL, c
  238.     MOV ES:[DI], AL
  239.   END;
  240. END;
  241.  
  242.  
  243. PROCEDURE ClearScreen(color : byte);
  244. var
  245.  Xres, Yres : longint;
  246.  number_of_segments : longint;
  247.  i : integer;
  248. begin
  249.  Xres := VESAModeInfo.Xresolution;
  250.  Yres := VESAModeInfo.Yresolution;
  251.  number_of_segments  := Round(((Xres *  Yres)/65536)+0.5)-1;
  252.  
  253.  for i := 0 to number_of_segments do
  254.   begin
  255.    SetBank(i*BankMult);
  256.    asm
  257.      mov     cx, 32768;
  258.      mov     ax,$A000
  259.      mov     es,ax
  260.      xor     di,di
  261.      mov     al,[color]
  262.      mov     ah,al
  263.      rep     stosw
  264.     end;
  265.   end;
  266. end;
  267.  
  268.  
  269.  
  270.  
  271.  
  272. Procedure SetCursor(X,Y : byte);
  273. Assembler;
  274. asm
  275.  mov ah,02h
  276.  xor bh,bh
  277.  mov dh,[Y]
  278.  mov dl,[X]
  279.  int 10h
  280. end;
  281.  
  282. Procedure WriteChar(chnr,color : byte);
  283. assembler;
  284. asm
  285.  mov ah,09h
  286.  mov al,[chnr]
  287.  mov bh,0
  288.  mov bl,[color]
  289.  mov cx,1
  290.  int 10h
  291. end;
  292.  
  293.  
  294.  
  295. Procedure WriteXY(x,y : integer;str : string; color : byte);
  296. var
  297.  i : byte;
  298. begin
  299.  SetCursor(x,y);
  300.  for i := 1 to length(str) do
  301.   begin
  302.    WriteChar(ord(str[i]),color);
  303.    SetCursor(X+i,Y);
  304.   end;
  305. end;
  306.  
  307.  
  308.  
  309. function ModeInList(nr : word) : boolean;
  310. var
  311.  i : integer;
  312.  found : boolean;
  313. begin
  314.  found := false;
  315.  for i := 0 to 40 do
  316.   if VESAModeList[i] = nr then found := true;
  317.  if found then ModeInList := true else ModeInList := false;
  318. end;
  319.  
  320.  
  321. Procedure Calc_variables;
  322. begin
  323. Case VESAModeInfo.WinGranularity of
  324.  1  : BankShiftModifier := 6;
  325.  2  : BankShiftModifier := 5;
  326.  4  : BankShiftModifier := 4;
  327.  8  : BankShiftModifier := 3;
  328.  16 : BankShiftModifier := 2;
  329.  32 : BankShiftModifier := 1;
  330.  64 : BankShiftModifier := 0;
  331. end;    {With granularities smaller than 64 but with page-size 64K the
  332.          banknr for each full segment of SVGA graphic will not be 0,1,2,3
  333.          but multiplied with BankMult ( 2^BankShiftModifier for easy bit
  334.          shifting)                                                         }
  335. BankMult := 64 div VESAModeInfo.WinGranularity;
  336. end;
  337.  
  338.  
  339.  
  340. Procedure WriteVESAInfo;
  341. var
  342.  i : integer;
  343. begin
  344.  for i := 0 to 3 do
  345.  Write(chr(VESAInfo.VESASignature[i]));
  346.  
  347.  Write(' ');
  348.  Writeln('Version : ',VESAInfo.VESAVersion shr 8,'.',VESAInfo.VESAVersion mod 256);
  349.  Writeln(VESAInfo.OEMStringPtr);
  350.  Writeln('Total Memory : ',VESAInfo.TotalMemory,' X 64Kb = '
  351.          ,VESAInfo.TotalMemory * 65536,' bytes = ',
  352.           VESAInfo.TotalMemory * 65536 div 1048576 ,'MB');
  353.  Writeln;
  354. end;
  355.  
  356. Procedure ModeList;
  357. var
  358.  i : integer;
  359.  p : ^word;
  360. begin
  361. Writeln('Modes Available : ');
  362. Writeln;
  363. for i := 0 to 40 do VESAModeList[i] := 0;
  364. i := 0;
  365. p := pointer(VESAInfo.VideoModePtr);
  366. while p^ <> $FFFF do
  367.  begin
  368.    if Not(ModeInList(P^)) then
  369.     begin
  370.      VESAModeList[i] := p^;
  371.      inc(i);
  372.      Write('$',Word2Hex(p^));
  373.      GetVESAModeInfo(p^);
  374.      write(' ',VESAModeInfo.Xresolution : 5,' X ',VESAModeInfo.Yresolution:3);
  375.      Write('   BPP : ',VESAModeInfo.BitsPerPixel:2);
  376.      if color in VESAModeInfo.ModeAttributes then Write(' Color') else
  377.         write(' Monocrome');
  378.      if graphic in VESAModeInfo.ModeAttributes then Write('  Graphic') else
  379.         write(' Textmode');
  380.  
  381.      if supported in VESAModeInfo.WinAAttributes then
  382.       begin
  383.        write(' Win A :');
  384.        if R in VESAModeInfo.WinAAttributes then Write(' R');
  385.        if W in VESAModeInfo.WinAAttributes then Write('W');
  386.        write(' at : $',Word2Hex(VESAModeInfo.WinASegment));
  387.       end;
  388.      if supported in VESAModeInfo.WinBAttributes then
  389.       begin
  390.        write(' Win B :');
  391.        if R in VESAModeInfo.WinBAttributes then Write(' R');
  392.        if W in VESAModeInfo.WinBAttributes then Write('W');
  393.        write(' at : $',Word2Hex(VESAModeInfo.WinASegment));
  394.       end;
  395.  
  396.      Write('  Gran : ',VESAModeInfo.WinGranularity:2,'Kb');
  397.      Writeln;
  398.     end;
  399.    inc(p);
  400.  end;
  401. end;
  402.  
  403.  
  404.  
  405.  
  406. BEGIN
  407. asm
  408.  mov ax,03h
  409.  int 10h
  410. end;
  411. Clrscr;
  412. Writeln('      ****************************************************************');
  413. Writeln('      *                                                              *');
  414. Writeln('      *                      SVGA using VESA 1.2                     *');
  415. Writeln('      *                        by : Telemachos                       *');
  416. Writeln('      *                                                              *');
  417. Writeln('      ****************************************************************');
  418. Writeln;
  419. Writeln('      Hiya! ');
  420. Writeln('      Welcome to the Peroxide Programming Tips #5');
  421. Writeln('      This one is on coding the SVGA card using VESA 1.2');
  422. Writeln('      This code *SHOULD* work on every graphic card that');
  423. Writeln('      supports VESA 1.2 (which is about every card now a days)');
  424. Writeln('                                                                      ');
  425. Writeln('      This program will :');
  426. Writeln('         1) Display information about your SVGA card');
  427. Writeln('         2) Display available modes on your card..');
  428. Writeln('         3) Cycle through the most common SVGA modes (256 colors)');
  429. Writeln('         4) Show you how to hardware scroll using the VESA BIOS');
  430. Writeln;
  431. Writeln('         Hit any key to get started....');
  432. Writeln;
  433. readkey;
  434.  
  435. clrscr;
  436.  
  437. If Not(GetVesaInfo) then Error('No VESA installed...');
  438.  
  439. WriteVESAInfo;
  440. ModeList;
  441. Writeln;
  442. Writeln('Press space to continue');
  443.  
  444.  
  445. readkey;
  446.  
  447. Randomize;
  448. if ModeInList($100) then
  449. begin
  450.  if NOT(SetVESAMode($100)) then exit;
  451.  GetVESAModeInfo($100);
  452.  Calc_variables;
  453.  ClearScreen(Random(255));
  454.  for counter := 0 to 10000 do
  455.   VESAPutPixel(Random(VESAModeInfo.Xresolution),
  456.                Random(VESAModeInfo.Yresolution),
  457.                Random(255));
  458.  WriteXY(5,2,'This is 640 X 400 X 256',50);
  459.  WriteXY(5,4,'Press space for next mode...',50);
  460.  fillchar(VESAModeInfo,256,0);
  461. end;
  462. readkey;
  463.  
  464. if ModeInList($101) then
  465. begin
  466.  if NOT(SetVESAMode($101)) then exit;
  467.  GetVESAModeInfo($101);
  468.  Calc_variables;
  469.  ClearScreen(Random(255));
  470.  for counter := 0 to 10000 do
  471.   VESAPutPixel(Random(VESAModeInfo.Xresolution),
  472.                Random(VESAModeInfo.Yresolution),
  473.                Random(255));
  474.  WriteXY(5,2,'This is 640 X 480 X 256',50);
  475.  WriteXY(5,4,'Press space for next mode...',50);
  476.  fillchar(VESAModeInfo,256,0);
  477. end;
  478. readkey;
  479.  
  480. if ModeInList($103) then
  481. begin
  482.  if NOT(SetVESAMode($103)) then exit;
  483.  GetVESAModeInfo($103);
  484.  Calc_variables;
  485.  ClearScreen(Random(255));
  486.  for counter := 0 to 10000 do
  487.   VESAPutPixel(Random(VESAModeInfo.Xresolution),
  488.                Random(VESAModeInfo.Yresolution),
  489.                Random(255));
  490.  WriteXY(5,2,'This is 800 X 600 X 256',50);
  491.  WriteXY(5,4,'Press space for next mode...',50);
  492.  fillchar(VESAModeInfo,256,0);
  493. end;
  494. readkey;
  495.  
  496. if ModeInList($105) then
  497. begin
  498.  if NOT(SetVESAMode($105)) then exit;
  499.  if Not(GetVESAModeInfo($105)) then Error('Ups.. noget galt her...');
  500.  Calc_variables;
  501.  ClearScreen(Random(255));
  502.  for counter := 0 to 10000 do
  503.   VESAPutPixel(Random(VESAModeInfo.Xresolution),
  504.                Random(VESAModeInfo.Yresolution),
  505.                Random(255));
  506.  WriteXY(5,2,'This is 1024 X 768 X 256',50);
  507.  WriteXY(5,4,'Press space for Hardware Scroll demo...',50);
  508.  fillchar(VESAModeInfo,256,0);
  509. end;
  510. readkey;
  511.  
  512. if ModeInList($101) then
  513. begin
  514.  if NOT(SetVESAMode($101)) then exit;
  515.  GetVESAModeInfo($101);
  516.  Calc_variables;
  517.  SetLogicalScanLine(1024);
  518.  VESAModeInfo.Xresolution := 1024;
  519.  SetScreenPosition(0,0);
  520.  
  521.  ClearScreen(Random(255));
  522.  for counter := 0 to 10000 do
  523.   VESAPutPixel(Random(VESAModeInfo.Xresolution),
  524.                Random(VESAModeInfo.Yresolution*2),
  525.                Random(255));
  526.  
  527.  for counter := 0 to 150 do SetScreenPosition(counter*2,0);
  528.  for counter := 150 downto 0 do SetScreenPosition(counter*2,0);
  529.  for counter := 0 to 150 do SetScreenPosition(counter*2,Counter*2);
  530.  for counter := 150 to 1040 do SetScreenPosition(300,counter*2);
  531.  
  532.  fillchar(VESAModeInfo,256,0);
  533. end
  534.  else
  535. WriteXY(5,7,'Mode $101 is unavailble on your card.. Skipping Scroll.',50);
  536.  
  537. delay(2000);
  538.  
  539. asm
  540.  mov ax,03h
  541.  int 10h
  542. end;
  543.  
  544. Writeln('All for now.. Signin'' out');
  545. Writeln('     Telemachos.');
  546. delay(5000);
  547. END.